﻿using Abp.EntityFrameworkCore;
using Abp.EntityFrameworkCore.Repositories;
using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using NetCore.Entities;
using NetCore.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCore.EntityFrameworkCore.Repositories
{
    /// <summary>
    /// Base class for custom repositories of the application.可以在这里扩展批量插入 
    /// 需要在模块中依赖注入泛型仓储来替换默认仓储没有的实现
    /// </summary>
    /// <typeparam name="TEntity">Entity type</typeparam>
    /// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam>
    public abstract class NetCoreRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<NetCoreDbContext, TEntity, TPrimaryKey>
        , INetCoreRepository<TEntity, TPrimaryKey>
        where TEntity : BaseEntity<TPrimaryKey> //class, IEntity<TPrimaryKey>
    {
        protected NetCoreRepositoryBase(IDbContextProvider<NetCoreDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        // 添加所有存储库的通用方法
        // Add your common methods for all repositories

        #region SqlServer Bulk
        /// <summary>
        /// sqlserver 批量插入
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task BulkInsertAsync(IList<TEntity> entities)
        {
            await base.Context.BulkInsertAsync(entities);
        }
        /// <summary>
        /// sqlserver 批量删除
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task BulkDeleteAsync(IList<TEntity> entities)
        {
            await base.Context.BulkDeleteAsync(entities);
        }

        #endregion


        #region PostgreSQL Bulk
        /// <summary>
        /// Messaia.Net.PostgreSQL.BulkExtensions 批量插入
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public void PostgreBulkInsert(IEnumerable<TEntity> entities)
        {
            base.Context.BulkInsert(entities);
        }
        #endregion
    }

    /// <summary>
    /// Base class for custom repositories of the application.
    /// This is a shortcut of <see cref="NetCoreRepositoryBase{TEntity,TPrimaryKey}"/> for <see cref="int"/> primary key.
    /// </summary>
    /// <typeparam name="TEntity">Entity type</typeparam>
    public class NetCoreRepositoryBase<TEntity> : NetCoreRepositoryBase<TEntity, int>
        , INetCoreRepository<TEntity>//IRepository<TEntity>//
        where TEntity : BaseEntity//class, IEntity<int>
    {
        public NetCoreRepositoryBase(IDbContextProvider<NetCoreDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        // Do not add any method here, add to the class above (since this inherits it)!!!
    }
}
